package com.next.androidintentlibrary;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;

public class GalleryIntents {

    private Context context;
    private Intent intent;

    private GalleryIntents(Context context)
    {
        this.context = context;
    }

    public static GalleryIntents from(Context context)
    {
        return new GalleryIntents(context);
    }

    public GalleryIntents openGallery()
    {
        intent = new Intent();
        intent.setAction("android.intent.action.MAIN");
        intent.addEntity("android.intent.category.APP_GALLERY");
        intent.setFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        // or
        // intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
        return this;
    }

    public GalleryIntents pickImage()
    {
        intent = new Intent();
        intent.setAction("android.intent.action.PICK");
        intent.setType("image/*");
        return this;
    }

    public Intent build()
    {
        return intent;
    }

    private void startAbility(Intent intent)
    {
        if (!(context instanceof Ability))
        {
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        }
        context.startAbility(intent,0);
    }

    public void show()
    {
        startAbility(build());
    }
}
